package com.han.hotelplat.service.systemuser.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.han.hotelplat.comm.base.EntityManage;
import com.han.hotelplat.comm.base.Pager;
import com.han.hotelplat.comm.utils.MD5;
import com.han.hotelplat.dao.systemuser.SystemuserDao;
import com.han.hotelplat.pojo.systemuser.Permission;
import com.han.hotelplat.pojo.systemuser.Resource;
import com.han.hotelplat.pojo.systemuser.Role;
import com.han.hotelplat.pojo.systemuser.Systemuser;
import com.han.hotelplat.service.systemuser.SystemuserService;

@Service
public class SystemuserServiceImpl implements SystemuserService{
	@Autowired
	private SystemuserDao systemuserDao;
	@Autowired
	private EntityManage entityManage;
	
	@Override
	public Map login(String username, String password) {
		Systemuser user = systemuserDao.findUserByUsername(username);
		Map<String, Object>data = new HashMap<String, Object>();
		if(user == null){
			data.put("code", -1);
			data.put("message", "用户名错误");
			return data;
		}
		if(!user.getPassword().equals(MD5.toMD5(password))){
			data.put("code", -1);
			data.put("message", "密码错误");
			return data;
		}
		data.put("code", 0);
		data.put("message", "登陆成功");
		data.put("user", user);
		return data;
	}

	@Override
	public void creatCMSUser(Systemuser user) {
		systemuserDao.save(user);
	}

	@Override
	public Systemuser findUserByTel(String tel) {
		return systemuserDao.findUserByTel(tel);
	}

	@Override
	public Systemuser findUserByUsernam(String username) {
		return systemuserDao.findUserByUsername(username);
	}

	@Override
	public void update(Systemuser user) {
		systemuserDao.update(user);
	}

	@Override
	public Systemuser findUserByUserid(String userid) {
		return systemuserDao.findUserByUserId(userid);
	}

	@Override
	public List<Permission> findAllPermissions() {
		return systemuserDao.findAllPermissions();
	}

	@Override
	public List<Resource> findAllResource() {
		return systemuserDao.findAllResource();
	}

	@Override
	public Pager<Systemuser> getAllSystemuserByPage(Integer areaCode,
			Integer roleId, String condition, Integer pageNo) {
		String hql = "from Systemuser where 1=1 ";
		/*if(areaCode != null && areaCode.intValue()>100000){//区域码至少6位
			String w_areaCode = areaCode.toString();
			int x=0;
			String w_area="";
			if(w_areaCode.endsWith("0000")) {
				//精度 省份
				x=9999;
			}else if(w_areaCode.endsWith("00")) {
				//精度 市
				x=99;
			}else {
				//精度 区
				x=0;
			}
			//要的   - 条件  >=0
			//410610-410000 <= 9999
			//410610-410600 <= 99
			//410610-410610 == 0
			w_area=" and convert(area,signed) - "+areaCode +" >=0 ";
			w_area+=" and convert(area,signed) - "+areaCode +" <= "+x;
			hql = hql +w_area;
		}*/
		Pager<Systemuser> data = entityManage.findByHqlForPage(hql, pageNo, (short)20);
		List<Systemuser> uli = new ArrayList<Systemuser>();
		for(Systemuser user : data.getList()){
			Role r = user.getRoles().size()>0?user.getRoles().get(0):null;
			Systemuser u = new Systemuser();
			u.setId(user.getId());
			u.setUserid(user.getUserid());
			u.setUsername(user.getUsername());
			u.setRealname(user.getRealname());
			u.setDepartment(user.getDepartment());
			u.setRolename(r == null ? "":r.getRolename());
			u.setRoleid(r == null ? -1:r.getRoleid());
			u.setTel(user.getTel());
			u.setPassword(null);
			u.setState(user.getState());
			uli.add(u);
		}
		data.setList(uli);
		return data;
	}

	@Override
	public Systemuser findUserById(Integer id) {
		return (Systemuser) entityManage.findById(Systemuser.class, id);
	}

}
